/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Clazz;
import me.zhengjie.modules.system.domain.Course;
import me.zhengjie.modules.system.domain.Schoolandclass;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.mapper.SchoolandclassMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.ClazzService;
import me.zhengjie.modules.system.domain.vo.ClazzQueryCriteria;
import me.zhengjie.modules.system.mapper.ClazzMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author tianjiaqi
* @date 2024-04-06
**/
@Service
@RequiredArgsConstructor
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    private final ClazzMapper clazzMapper;

    private final UserMapper userMapper;

    private final SchoolandclassMapper schoolandclassMapper;


    public List<Clazz> queryClazzList(List<Long> ids){

        String result = String.join(", ", ids.stream().map(Object::toString).collect(Collectors.toList()));
        if(ids.size()!=0){
            return clazzMapper.queryClazzList(ids);
        } else {
            return new ArrayList<>();
        }

    }

    @Override
    public PageResult<Clazz> queryAll(ClazzQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(clazzMapper.findAll(criteria, page));
    }

    public Clazz queryClazzMsg(String clazzId){
        return clazzMapper.selectById(clazzId);
    }

    @Override
    public List<Clazz> queryAll(ClazzQueryCriteria criteria){
        return clazzMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Clazz resources) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {

            String currentUserName = authentication.getName();
            User currentUser = userMapper.findByUsername(currentUserName);
            resources.setSchoolid(currentUser.getDeptId());

            Schoolandclass schoolandclass = new Schoolandclass();
            save(resources);

            schoolandclass.setClassId(resources.getId());
            schoolandclass.setSchoolId(currentUser.getDeptId());
            schoolandclassMapper.insert(schoolandclass);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Clazz resources) {
        Clazz clazz = getById(resources.getId());
        clazz.copy(resources);
        saveOrUpdate(clazz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        System.out.println(ids);
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Clazz> all, HttpServletResponse response) throws IOException {

        List<Map<String, Object>> list = new ArrayList<>();
        for (Clazz clazz : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("班级", clazz.getName());
            map.put("简介", clazz.getInfo());
            map.put(" lastupdatetime",  clazz.getLastupdatetime());
            map.put("创建时间", clazz.getCreatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}